<?php

require_once('Joob/Http/Request/Parser/Interface.php');

/**
 * 
 * Simple request parser based on HTTP parameters
 * @author griever
 *
 */
class Joob_Http_Request_Parser_Parameters implements Joob_Http_Request_Parser_Interface {
	
	/**
	 * Extract component, module and task from HTTP parameters
	 * @param mod module (optional) the requested module, default is "index"
	 * @param task task (optional) the requested task, default is "index"
	 * @param com component (optional) the requested component, default is null
	 * @see Joob_Http_Request_Parser_Interface::parseRequest()
	 */
	public function parseRequest($request) {
		if (!$request->hasParam('com'))	{
			$com = "default";
		}
		if (!$request->hasParam('mod'))	{
			$request->setParam('mod', 'index');
		}
		if (!$request->hasParam('task'))	{
			$request->setParam('task', 'index');
		}
		
		$com = $request->getParam('com');
		$mod = $request->getParam('mod');
		$task = $request->getParam('task');
		
		if (!$this->checkValid($com))	{
			$com = "default";
		}

		if (!$this->checkValid($mod))	{
			$mod = "index";
		}
		
		if (!$this->checkValid($task))	{
			$task = "index";
		}
		
		$request->setComponentName($com);
		$request->setModuleName($mod);
		$request->setTaskName($task);
		
		return $request;
	}
	
	protected function checkValid($param)	{
		if (preg_match("/[^a-zA-Z\\-]/", $param))	{
			return false;
		}
		return true;
	}
}

?>